package com.baymax.test.scene.internal

import android.content.Context
import android.util.AttributeSet
import android.view.KeyEvent
import android.view.View

class FocusIdEditeTextView : androidx.appcompat.widget.AppCompatEditText,
    View.OnKeyListener {
    constructor(context: Context) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context!!,
        attrs,
        defStyleAttr
    )


    init {
        setOnKeyListener(this)
    }

    override fun onKey(v: View?, keyCode: Int, event: KeyEvent): Boolean {
        if (event.action == KeyEvent.ACTION_DOWN) {
            if (nextFocusRightId != View.NO_ID && keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
                return  rootView.findViewById<View>(nextFocusRightId)?.requestFocus() ?: false
            }
            if (nextFocusLeftId != View.NO_ID && keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
                return  rootView.findViewById<View>(nextFocusLeftId)?.requestFocus() ?: false
            }
            if (nextFocusUpId != View.NO_ID && keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                return  rootView.findViewById<View>(nextFocusUpId)?.requestFocus() ?: false
            }
            if (nextFocusDownId != View.NO_ID && keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                return  rootView.findViewById<View>(nextFocusDownId)?.requestFocus() ?: false
            }
            if (nextFocusForwardId != View.NO_ID && keyCode == KeyEvent.KEYCODE_FORWARD) {
                return  rootView.findViewById<View>(nextFocusForwardId)?.requestFocus() ?: false
            }
        }
        return false
    }


}